package com.gw.quotes.activity;

import net.tsz.afinal.FinalActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;

import com.gw.quotes.AppInfo;
import com.gw.quotes.AppManager;
import com.gw.quotes.MyApplication;
import com.gw.quotes.R;
import com.gw.quotes.entity.system.User;
import com.gw.quotes.util.ACache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.YXWProgressDialog.YXWDialog;

public class BaseActivity extends FinalActivity {
	protected Resources resources;
	protected YXWDialog dialog;
	protected ACache aCache;
	protected ImageLoader imageLoader;
	protected DisplayImageOptions options;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// 去掉窗口标题,必须放在onCreate前面
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		// 添加Activity到堆栈
		AppManager.getAppManager().addActivity(this);

		// 初始化dialog
		dialog = new YXWProgressDialog(this).createDialog("加载中…");
		resources = getResources();
		// 获取手机的分辨率
		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		AppInfo.SCREEN_WIDTH = dm.widthPixels;
		AppInfo.SCREEN_HEIGHT = dm.heightPixels;
		AppInfo.SCREEN_DENSITY = dm.density;
		aCache = ACache.get(this);

		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(this));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_empty).cacheInMemory(true)
				.cacheOnDisk(true).displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.ARGB_8888).build();
	}

	protected User getLoginUser() {
		return MyApplication.instance().getLoginUser();
	}

	/**
	 * 自定义Activity切换动画
	 * 
	 * @param intent
	 * @param enterAnim
	 * @param exitAnim
	 */
	protected void startActivityAnim(Intent intent, int enterAnim, int exitAnim) {
		startActivity(intent);
		overridePendingTransition(enterAnim, exitAnim);
	}

	/**
	 * 默认Activity放大动画
	 * 
	 * @param intent
	 */
	// protected void startActivityAnim(Intent intent) {
	// startActivity(intent);
	// overridePendingTransition(R.anim.uc_alpha_in, R.anim.uc_alpha_out);
	// }
	/**
	 * 默认带返回的Activity放大动画
	 * 
	 * @param intent
	 * @param requestCode
	 */
	// protected void startActivityAnimForResult(Intent intent, int requestCode)
	// {
	// startActivityAnimForResult(intent,requestCode,R.anim.uc_alpha_in,
	// R.anim.uc_alpha_out);
	// }
	/**
	 * 自定义带返回的Activity切换动画
	 * 
	 * @param intent
	 * @param requestCode
	 * @param enterAnim
	 * @param exitAnim
	 */
	protected void startActivityAnimForResult(Intent intent, int requestCode,
			int enterAnim, int exitAnim) {
		startActivityForResult(intent, requestCode);
		overridePendingTransition(enterAnim, exitAnim);
	}

	/**
	 * finish时默认缩放小时
	 */
	// @Override
	// public void finish() {
	// // TODO Auto-generated method stub
	// super.finish();
	// overridePendingTransition(R.anim.uc_alpha_in,
	// R.anim.uc_alpha_out_finish);
	// }
	/**
	 * 原始的finish动画
	 */
	public void finishDefault() {
		super.finish();
	}

	/**
	 * 自定义finish动画
	 * 
	 * @param enterAnim
	 * @param exitAnim
	 */
	protected void finishActivityAnim(int enterAnim, int exitAnim) {
		super.finish();
		overridePendingTransition(enterAnim, exitAnim);
	}

	public void goback(View v) {
		this.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		// 结束Activity&从堆栈中移除
		AppManager.getAppManager().finishActivity(this);
	}

}
